package com.zqh.categoryquery.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqh.categoryquery.dao.CategoryDao;
import com.zqh.categoryquery.service.CategoryQueryService;
import com.zqh.myfmmall.clients.ProductQueryClient;
import com.zqh.myfmmall.entity.Category;
import com.zqh.myfmmall.result.Result;
import com.zqh.myfmmall.vo.CategoryVo;
import com.zqh.myfmmall.vo.ProductVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Description :
 * @Author :kono淇杭哒、
 * @Create 2022-02-18 2:19
 */
@Service("categoryQueryService")
public class CategoryQueryServiceImpl extends ServiceImpl<CategoryDao, Category> implements CategoryQueryService{
    @Resource
    private CategoryDao categoryDao;
    @Autowired
    private ProductQueryClient productQueryClient;

    @Override
    public List<CategoryVo> selectVoByParentId(String parentId) {
        List<CategoryVo> categoryVos = categoryDao.selectVoByParentId(parentId);
        return categoryVos;
    }

    @Override
    public Result selectFirstLevelCategoryVo() {
        List<CategoryVo> categoryVos = selectAllCategorieVo();
        for (CategoryVo categoryVo : categoryVos) {
            Result result = productQueryClient.rootCategoryHighestSelling(categoryVo.getCategoryId());
            List<ProductVo> productVos = JSON.parseArray(JSON.toJSONString(result.getData()), ProductVo.class);
            categoryVo.setRecommendedProducts(productVos);
        }
        return Result.success(categoryVos);
    }

    @Override
    public List<CategoryVo> selectAllCategorieVo(){
        List<CategoryVo> categoryVos = selectVoByParentId("0");
        return categoryVos;
    }
}
